package com.jeasonzhao.commons.chs;

public class RegionInitData
{
    public RegionInitData()
    {
    }

    private static java.util.Hashtable<String,String> provincesss = new java.util.Hashtable<String,String>();
    private static java.util.Hashtable<String,String> zips = new java.util.Hashtable<String,String>();
    private static java.util.Hashtable<String,String> tolls = new java.util.Hashtable<String,String>();
    private static void buildHash()
        throws Exception
    {
        String files = "d:\\codes.txt";
        java.io.BufferedReader buf = new java.io.BufferedReader(new java.io.FileReader(files));
        String line = null;
        String province = null;
        while(null != (line = buf.readLine()))
        {
            if(line.trim().length() < 1)
            {
                continue;
            }
            if(line.endsWith("=="))
            {
                province = line.substring(0,line.indexOf("=")).trim();
                provincesss.put(province,province);
            }
            else
            {
                String[] args = line.split("\\s+");
                for(int n = 0;n < args.length && (n + 3) <= args.length;n += 3)
                {
                    String key = province.trim() + ">" + args[n].trim();
                    //System.out.println(key+">"+args[n+1]+">"+args[n+2]);
                    zips.put(key,args[n + 1]);
                    tolls.put(key,args[n + 2]);
                }
            }
        }
        buf.close();
    }

    public static void main(String[] argvs)
        throws Exception
    {
        buildHash();
        regions();
    }

    private static void regions()
        throws Exception
    {
        String files = "d:\\regions.txt";
        java.io.BufferedReader buf = new java.io.BufferedReader(new java.io.FileReader(files));
        String line = null;
        String root = "[CHINA]";
        String provinceName = null;
        String cityName=null;
        String parenttoll = null;
        String parentzip = null;
        while(null != (line = buf.readLine()))
        {
            if(line.trim().length() < 1)
            {
                continue;
            }
            line = line.trim();
            String section = line.substring(0,6).trim();
            String name = line.substring(6).trim();
            String province = section.substring(0,2);
            String city = section.substring(2,4);
            String country = section.substring(4);
            String parentCode = null;
            String fullName=null;
            RegionType type = RegionType.City;
            RegionDetailedType detailedType = RegionDetailedType.City;
            String key = provinceName + ">" + name.trim();
            String zipcode = zips.get(key);
            String toll = tolls.get(key);
//            int level = 0;
            if(city.equals("00") && country.equals("00"))
            {
//                level = 0;
                parentCode = root;
                type = RegionType.Province;

                //第一层为AB两位代码表示省、自治区、直辖市、特别行政区；
                if(name.endsWith("市"))
                {
                    key = name + ">" + name.trim();
                    zipcode = zips.get(key);
                    toll = tolls.get(key);
                    detailedType = RegionDetailedType.Municipality;
                }
                else if(name.endsWith("自治区"))
                {
                    key = name;
                    detailedType = RegionDetailedType.Municipality;
                }
                else if(name.endsWith("省"))
                {
                    key = name;
                    detailedType = RegionDetailedType.Province;
                }
                provinceName = name;
                fullName=name;
            }
            else if(country.equals("00"))
            {
//                level = 1;
                parentCode = province + "0000";
                type = RegionType.City;
                //第二层为CD两位代码表示市、地区、自治州、盟、直辖市所辖市辖区、县汇总码、省（自治区）直辖县级行政区划汇总码，其中：
                //——01~20、51~70表示市，01、02还用于表示直辖市所辖市辖区、县汇总码；
                //——21~50表示地区、自治州、盟；
                //——90表示省（自治区）直辖县级行政区划汇总码。
                if((city.equals("01") || city.equals("02")) && name.equals("县"))
                {
                    detailedType = RegionDetailedType.MunicipalCountry;
                    zipcode = parentzip;
                    toll = parenttoll;
                    fullName=provinceName+" 郊县";
                    cityName=null;
                }
                else if((city.equals("01") || city.equals("02") || city.equals("03"))
                        && (name.equals("市辖区") || name.equals("市")))
                {
                    detailedType = RegionDetailedType.MunicipalDistrict;
                    zipcode = parentzip;
                    toll = parenttoll;
                    fullName=provinceName+" 市区";
                    cityName=null;
                }
                else if("21".compareTo(city) <= 0 && "50".compareTo(city) >= 0)
                {
                    detailedType = RegionDetailedType.AutonomousDistrict;
                    fullName=provinceName+" "+name;
                    cityName=name;
                }
                else if("90".equals(city))
                {
                    detailedType = RegionDetailedType.MunicipalCountrySummary;
                    zipcode = parentzip;
                    toll = parenttoll;
                    fullName = provinceName + " 县级市";
                    cityName=null;
                }
                else //if(("51".compareTo(city)<=0 &&"70".compareTo(city)>=0)||("01".compareTo(city)<=0 &&"20".compareTo(city)>=0))
                {
                    detailedType = RegionDetailedType.City;
                    fullName=provinceName+" "+name;
                    cityName=name;
                }
            }
            else
            {
//                level = 2;
                parentCode = province + city + "00";
                type = RegionType.Country;
                //第三层为EF两位表示县、自治县、县级市、旗、自治旗、市辖区、林区、特区，其中：
                //——01~20表示市辖区、地区（自治州、盟）辖县级市、市辖特区以及省（自治区）直辖县级行政区划中的县级市，01通常表示辖区汇总码；
                //——21~80表示县、自治县、旗、自治旗、林区、地区辖特区；
                //——81~99表示省（自治区）辖县级市。
                if("市辖区".equals(name))
                {
                    detailedType = RegionDetailedType.MunicipalDistrict;
                    toll = parenttoll;
                    type = RegionType.District;
                }
                else if("01".compareTo(country) <= 0 && "20".compareTo(country) >= 0)
                {
                    detailedType = city.equals("90") ? RegionDetailedType.City : RegionDetailedType.District;
                    type = city.equals("90") ? RegionType.Country : RegionType.District;
                    toll = parenttoll;
                }
                else if("21".compareTo(country) <= 0 && "80".compareTo(country) >= 0)
                {
                    detailedType = RegionDetailedType.Country;
                    toll = parenttoll;
                }
                else
                {
                    detailedType = RegionDetailedType.CountryLevelCity;
                    toll = parenttoll;
                }
                fullName=provinceName+(null==cityName?"":(" "+cityName))+" "+name;
            }
            if(detailedType == RegionDetailedType.MunicipalCountry
               || detailedType == RegionDetailedType.MunicipalCountrySummary
               || detailedType == RegionDetailedType.MunicipalDistrict)
            {
                toll = null;
                zipcode = null;
            }

//           if(level==0 && provincesss.containsKey(provinceName)==false)
//            if(level == 2 && toll == null)
//                System.out.println("<region"
//                                   + " fullName=\""+fullName+"\""
//                                   + " code=\"" + section + "\""
//                                   + " name=\"" + name + "\""
//                                   + " zipcode=\"" + (null == zipcode ? "" : zipcode) + "\""
//                                   + " toll=\"" + (null == toll ? "" : toll) + "\""
//                                   + " parentCode=\"" + parentCode + "\""
//                                   + " type=\"" + type + "\""
//                                   + " detailedType=\"" + detailedType + "\""
//                                   + " level=\"" + level + "\""
//                                   + "/>"
//                );

//            System.out.println("regions.add(new Region(\""+parentCode+"\",\"" + section + "\",\"" + name + "\",RegionType." + type
//                               + ",RegionDetailedType." + detailedType
//                               + "," + (null == toll ? "null" : ("\"" + toll + "\""))
//                               + "," + (null == zipcode ? "null" : ("\"" + zipcode + "\"")) + "));"
//                );
            if(null==toll)toll="";
            if(null==zipcode)zipcode="";
            String sql="insert into SEC_Region(id,PARENTID,name,TYPE,DETAILEDTYPE,fullName,TOLL,ZIPCODE)"
                +"values('"+section+"','"+parentCode+"','"+name+"','"+type+"','"+detailedType
                +"','"+fullName+"','"+toll+"','"+zipcode+"');";
            System.out.println(sql);
            if(null != toll)
            {
                parenttoll = toll;
            }
            if(null != zipcode)
            {
                parentzip = zipcode;
            }
            //System.out.println(key+">"+section+"="+name+" "+zipcode+" "+toll);
            //break;
        }
    }

}
